package com.qqbot.impl.task;

import com.qqbot.core.core.Bot;
import com.qqbot.core.core.BotFactory;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.DuJiaoShouApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 每日定时推送新闻
 *
 * @author changwenpeng
 */
@Component
public class RobotTaskNews {

    @Value("${mybot.morningNewGroups}")
    Long[] morningNewGroups;

    @Autowired
    DuJiaoShouApiService duJiaoShouApiService;

    /**
     * 每日新闻
     *
     * @throws Exception
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void morningNews() throws Exception {
        MessageChain mc = duJiaoShouApiService.news();
        this.sendMorningNews(mc);
    }

    /**
     * 发送消息
     *
     * @param mc
     * @throws Exception
     */
    public void sendMorningNews(MessageChain mc) {
        List<Bot> botList = BotFactory.getBots();
        if (CollectionUtils.isEmpty(botList)) {
            return;
        }
        Bot bot = botList.get(0);
        for (Long groupId : morningNewGroups) {
            System.out.println(mc.toMessageString());
            bot.sendGroupMessage(groupId, mc);
        }
    }

}
